package com.example.myapplication.ui

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.myapplication.ui.download.FileDownloadBean
import com.example.myapplication.ui.download.FileDownloadDao
import com.example.myapplication.ui.upload.FileUploadBean
import com.example.myapplication.ui.upload.FileUploadDao
import com.example.myapplication.ui.viewmodel.UploadDao

@Database(
    entities = [FileDownloadBean::class, FileUploadBean::class],
    version = 1,
    exportSchema = false
)
abstract class FileRoomDatabase : RoomDatabase() {

    abstract fun fileDownloadDao(): FileDownloadDao

    abstract fun uploadDao() : UploadDao

    abstract fun fileUploadDao(): FileUploadDao

    companion object {
        @Volatile
        private var INSTANCE: FileRoomDatabase? = null

        /**
         * 获取room数据库
         */
        fun getDatabase(context: Context): FileRoomDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    FileRoomDatabase::class.java,
                    "file_database"
                )
                    //.createFromAsset("database/default.db")
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }
}